#!/bin/env perl

#
# Prelude.
#

use iSOCK::Test::Prelude;

BEGIN {
  use iSOCK::Test::Setup;
  
  iSOCK::Test::Setup->setup;
}

#
# Modules.
#

use File::Slurp qw( slurp );
use JSON;
use YAML qw( Dump DumpFile );

use SOCK::TDD;
use iSOCK;
use iSOCK::Model::App;
use iSOCK::Controller::Root;

#
# TDD.
#

my $tdd = SOCK::TDD->new;
my $ttt = $tdd->path;
my $tmp = $tdd->tmp;

#
# Plan.
#

$tdd->plan ( tests => ( 2 + 2 + 1 ) + ( 2 + 1 ) + ( 2 * 2 ) );

#
# Core.
#

my $schema = iSOCK::Model::App->new;

for my $product ( qw( 63906-A ) ) {
  system ( "psql --dbname=app-test --file=$ttt/$product.sql" ) == 0 or $tdd->BAIL_OUT ( "Could not initialize test DB!" );
  
  test ( $product, '0', $_ ) for qw( integration specification );
  test ( $product, '1', $_ ) for qw( integration specification );
  
  {
    my $loh = $schema->product_components_loh ( $product );
    
    my $got_file      = "$tmp/$product-X.yaml";
    my $expected_file = "$ttt/$product-X.yaml";
    
    delete $_->{integration_changed}   for @$loh;
    delete $_->{specification_changed} for @$loh;
    
    DumpFile ( $got_file, $loh );
    
    $tdd->is_abysmally ( $got_file, $expected_file, "$product [X] (product x components)", { slurp_files => 1, unyaml_files => 1 } );
  }
}

for my $product ( qw( 63906-B ) ) {
  system ( "psql --dbname=app-test --file=$ttt/$product.sql" ) == 0 or $tdd->BAIL_OUT ( "Could not initialize test DB!" );
  
  test ( $product, '0', 'integration' );
  test ( $product, '1', 'integration' );
}

$tdd->is_abysmally (
  [ map { $_->component_version } $schema->resultset ( 'Freeze' )->search ( { component_id => 'ACC' }, { order_by => 'component_version' } )->all ],
  [ qw( 1.0.4 1.0.5 2.0.0 ) ],
  'ACC freezes were left unaffected cross-product-wise',
);

#
# Products X Components.
#

for ( qw( integration specification ) ) {
  my $lol = $schema->product_to_component_lol ( $_ );
  
  {
    my $got_file      = "$tmp/product_to_component-$_.yaml";
    my $expected_file = "$ttt/product_to_component-$_.yaml";

    DumpFile ( $got_file, $lol );

    $tdd->is_abysmally ( $got_file, $expected_file, "products x components ($_, YAML)", { slurp_files => 1, unyaml_files => 1 } );
  }
  
  {
    $lol = iSOCK::Controller::Root->_htmlize_lol ( $lol );
    
    my $got_file      = "$tmp/product_to_component-$_-html.yaml";
    my $expected_file = "$ttt/product_to_component-$_-html.yaml";

    DumpFile ( $got_file, $lol );

    $tdd->is_abysmally ( $got_file, $expected_file, "products x components ($_, HTML)", { slurp_files => 1, unyaml_files => 1 } );
  }
}

#
# Subs.
#

sub test {
  my ( $product, $number, $what ) = @_;

  my $json    = decode_json ( slurp ( "$ttt/$product-$number-$what.json" ) );
  my $actions = $schema->feed_component_lineup ({ data => $json });

  my $got_file      = "$tmp/$product-$number-$what.yaml";
  my $expected_file = "$ttt/$product-$number-$what.yaml";

  DumpFile ( $got_file, $actions );

  return $tdd->is_abysmally ( $got_file, $expected_file, "$product [$number] (feed $what)", { slurp_files => 1, unyaml_files => 1 } );
}
